package com.fqh.controller;

import com.fqh.entity.Diningtable;
import com.fqh.entity.JsonResult;
import com.fqh.entity.Login;
import com.fqh.service.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Scope(value = "prototype")
@Controller
public class TableController {

    @Autowired
    private TableService tableService;

    @RequestMapping("/findAllTable.do")
    @ResponseBody
    public List findAllTable() {
        List<Diningtable> list = tableService.findAllTable();
        return list;
    }

    @RequestMapping("/orderTable.do")
    @ResponseBody
    public JsonResult orderTable(HttpServletRequest request, @RequestParam("tableNum") String tableNum, @RequestParam("people") Integer people) {
        JsonResult result = null;
        try {
            tableService.updateTableState(tableNum);
            HttpSession session = request.getSession();
            Login user = (Login) session.getAttribute("user");
            session.setAttribute("tableNum", tableNum);
            tableService.addInfo(request, user.getLoginId(), people, tableNum);
            result = new JsonResult("成功", true);
        } catch (Exception e) {
            result = new JsonResult("失败", false);
            e.printStackTrace();
        }
        return result;
    }
}
